package com.sec.bnjey.activity.dialog;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;

import com.sec.bnjey.R;

public class IKPopup extends Dialog {
	public static final int POPUP_ERRORCODE_CLEAR_COMPLETE = 0x1;
	public static final int POPUP_CEHCK_CAR = 0x2;

	public static final int POPUP_INFO_ONE_BTN = 0x6;
	public static final int POPUP_INFO_TWO_BTN = 0x7;
	public static final int POPUP_INFO_TWO_BTN_SN = 0x0;
	public static final int POPUP_ALERT_ONE_BTN = 0x8;
	public static final int POPUP_ALERT_TWO_BTN = 0x9;

	private static Activity currentActivity = null;

	public IKPopup(Context context, int type) {
		super(context, type);
		setOwnerActivity((Activity) context);

		currentActivity = (Activity) context;
		setPosition(0);
	}

	@Override
	public boolean isShowing() {
		return super.isShowing();
	}

	public void setPosition(int y) {
		WindowManager.LayoutParams lp = getWindow().getAttributes();
		getWindow().setGravity(Gravity.CENTER);
		getWindow().setAttributes(lp);
	}

	public static class Builder {
		private Context mContext;
		private String mMessage;
		private String mTitle;
		private View mContentView;
		private int mType;
		public onIKPopupClickListener mOnIKPopupClickListener;

		private String mBtnText1;
		private String mBtnText2;

		public interface onIKPopupClickListener {
			public void onIKPopUpClick(View v);

		}

		public Builder(Context context, int type) {
			this.mContext = context;
			mType = type;
		}

		public Builder(Context context, int type, String title, String message,
				String btnText1, String btnText2) {
			this.mContext = context;
			mType = type;
			mTitle = title;
			mMessage = message;
			mBtnText1 = btnText1;
			mBtnText2 = btnText2;
		}

		public Builder(Context context, int type, String title, String message,
				String btnText1) {
			this.mContext = context;
			mType = type;
			mTitle = title;
			mMessage = message;
			mBtnText1 = btnText1;
		}

		public Builder(Context context, int type, String title, String message) {
			this.mContext = context;
			mMessage = message;
			mTitle = title;
			mType = type;
		}

		public void setOnIKPopuoClickListener(
				onIKPopupClickListener ikPopupClickListener) {
			mOnIKPopupClickListener = ikPopupClickListener;
		}

		public View getCheckAnimationView() {
			return mContentView;
		}

		public IKPopup create() {
			LayoutInflater inflater = (LayoutInflater) mContext
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			switch (mType) {
			case POPUP_CEHCK_CAR: {
				final IKPopup dialog = new IKPopup(mContext,
						R.style.Style_ErroCode_Clear);
				mContentView = inflater.inflate(R.layout.popup_check_layout,
						null);
				dialog.addContentView(mContentView, new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				dialog.setContentView(mContentView);
				Button b = (Button) mContentView
						.findViewById(R.id.B_Popup_Cancel);
				b.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mOnIKPopupClickListener != null)
							mOnIKPopupClickListener.onIKPopUpClick(v);
					}
				});
				return dialog;
			}
			case POPUP_ERRORCODE_CLEAR_COMPLETE: {
				final IKPopup dialog = new IKPopup(mContext,
						R.style.Style_ErroCode_Clear);
				mContentView = inflater.inflate(
						R.layout.popup_codeclear_complete_layout, null);
				dialog.addContentView(mContentView, new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				dialog.setContentView(mContentView);
				return dialog;
			}

			case POPUP_INFO_ONE_BTN: {
				final IKPopup dialog = new IKPopup(mContext,
						R.style.Style_ErroCode_Clear);
				mContentView = inflater.inflate(R.layout.popup_one_btn_layout,
						null);

				mContentView.findViewById(R.id.IV_PopupTitle)
						.setBackgroundResource(R.drawable.icon_popup_notice);
				mContentView.findViewById(R.id.RL_OneBtnPopup_Top)
						.setBackgroundResource(R.drawable.popup_notice_topbg);
				mContentView
						.findViewById(R.id.RL_OneBtnPopup_Body)
						.setBackgroundResource(R.drawable.popup_notice_centerbg);
				mContentView
						.findViewById(R.id.RL_OneBtnPopup_Bottom)
						.setBackgroundResource(R.drawable.popup_notice_bottombg);

				dialog.addContentView(mContentView, new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				TextView tv = (TextView) mContentView
						.findViewById(R.id.TV_PopupTitle);
				if (mTitle != null)
					tv.setText(mTitle);
				tv = (TextView) mContentView.findViewById(R.id.TV_PopupMessage);
				if (mMessage != null)
					tv.setText(mMessage);
				dialog.setContentView(mContentView);
				Button b = (Button) mContentView.findViewById(R.id.B_Popup_Ok);
				if (mBtnText1 != null)
					b.setText(mBtnText1);
				b.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mOnIKPopupClickListener != null)
							mOnIKPopupClickListener.onIKPopUpClick(v);
					}
				});
				return dialog;
			}
			case POPUP_INFO_TWO_BTN: {
				final IKPopup dialog = new IKPopup(mContext,
						R.style.Style_ErroCode_Clear);
				mContentView = inflater.inflate(R.layout.popup_two_btn_layout,
						null);

				mContentView.findViewById(R.id.IV_PopupTitle)
						.setBackgroundResource(R.drawable.icon_popup_notice);
				mContentView.findViewById(R.id.RL_TwoBtnPopup_Top)
						.setBackgroundResource(R.drawable.popup_notice_topbg);
				mContentView
						.findViewById(R.id.RL_TwoBtnPopup_Body)
						.setBackgroundResource(R.drawable.popup_notice_centerbg);
				mContentView
						.findViewById(R.id.RL_TwoBtnPopup_Bottom)
						.setBackgroundResource(R.drawable.popup_notice_bottombg);

				dialog.addContentView(mContentView, new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				TextView tv = (TextView) mContentView
						.findViewById(R.id.TV_PopupTitle);
				if (mTitle != null)
					tv.setText(mTitle);
				tv = (TextView) mContentView.findViewById(R.id.TV_PopupMessage);
				if (mMessage != null)
					tv.setText(mMessage);
				dialog.setContentView(mContentView);

				Button b = (Button) mContentView.findViewById(R.id.B_Popup_Ok);

				if (mBtnText1 != null)
					b.setText(mBtnText1);
				b.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mOnIKPopupClickListener != null)
							mOnIKPopupClickListener.onIKPopUpClick(v);
					}
				});

				b = (Button) mContentView.findViewById(R.id.B_Popup_Cancel);

				if (mBtnText2 != null)
					b.setText(mBtnText2);
				b.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mOnIKPopupClickListener != null)
							mOnIKPopupClickListener.onIKPopUpClick(v);
					}

				});
				return dialog;
			}

			case POPUP_INFO_TWO_BTN_SN: {
				final IKPopup dialog = new IKPopup(mContext,
						R.style.Style_ErroCode_Clear);
				mContentView = inflater.inflate(R.layout.popup_sn_layout, null);

				mContentView.findViewById(R.id.IV_PopupTitle)
						.setBackgroundResource(R.drawable.icon_popup_notice);
				mContentView.findViewById(R.id.RL_TwoBtnPopup_Top)
						.setBackgroundResource(R.drawable.popup_notice_topbg);
				mContentView
						.findViewById(R.id.RL_TwoBtnPopup_Body)
						.setBackgroundResource(R.drawable.popup_notice_centerbg);
				mContentView
						.findViewById(R.id.RL_TwoBtnPopup_Bottom)
						.setBackgroundResource(R.drawable.popup_notice_bottombg);

				dialog.addContentView(mContentView, new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				TextView tv = (TextView) mContentView
						.findViewById(R.id.TV_PopupTitle);
				if (mTitle != null)
					tv.setText(mTitle);
				tv = (TextView) mContentView.findViewById(R.id.TV_PopupMessage);
				if (mMessage != null)
					tv.setText(mMessage);
				dialog.setContentView(mContentView);

				Button b = (Button) mContentView.findViewById(R.id.B_Popup_Ok);

				if (mBtnText1 != null)
					b.setText(mBtnText1);
				b.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mOnIKPopupClickListener != null)
							mOnIKPopupClickListener.onIKPopUpClick(v);
					}
				});

				b = (Button) mContentView.findViewById(R.id.B_Popup_Cancel);

				if (mBtnText2 != null)
					b.setText(mBtnText2);
				b.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mOnIKPopupClickListener != null)
							mOnIKPopupClickListener.onIKPopUpClick(v);
					}

				});
				return dialog;
			}

			case POPUP_ALERT_ONE_BTN: {
				final IKPopup dialog = new IKPopup(mContext,
						R.style.Style_ErroCode_Clear);
				mContentView = inflater.inflate(R.layout.popup_one_btn_layout,
						null);

				mContentView.findViewById(R.id.IV_PopupTitle)
						.setBackgroundResource(R.drawable.icon_popup_alert);
				mContentView.findViewById(R.id.RL_OneBtnPopup_Top)
						.setBackgroundResource(R.drawable.popup_alert_topbg);
				mContentView.findViewById(R.id.RL_OneBtnPopup_Body)
						.setBackgroundResource(R.drawable.popup_alert_centerbg);
				mContentView.findViewById(R.id.RL_OneBtnPopup_Bottom)
						.setBackgroundResource(R.drawable.popup_alert_bottombg);

				dialog.addContentView(mContentView, new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				TextView tv = (TextView) mContentView
						.findViewById(R.id.TV_PopupTitle);
				if (mTitle != null)
					tv.setText(mTitle);
				tv = (TextView) mContentView.findViewById(R.id.TV_PopupMessage);
				if (mMessage != null)
					tv.setText(mMessage);
				dialog.setContentView(mContentView);
				Button b = (Button) mContentView.findViewById(R.id.B_Popup_Ok);
				if (mBtnText1 != null)
					b.setText(mBtnText1);
				b.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mOnIKPopupClickListener != null)
							mOnIKPopupClickListener.onIKPopUpClick(v);
					}
				});
				return dialog;
			}
			case POPUP_ALERT_TWO_BTN: {
				final IKPopup dialog = new IKPopup(mContext,
						R.style.Style_ErroCode_Clear);
				mContentView = inflater.inflate(R.layout.popup_two_btn_layout,
						null);

				mContentView.findViewById(R.id.IV_PopupTitle)
						.setBackgroundResource(R.drawable.icon_popup_alert);
				mContentView.findViewById(R.id.RL_TwoBtnPopup_Top)
						.setBackgroundResource(R.drawable.popup_alert_topbg);
				mContentView.findViewById(R.id.RL_TwoBtnPopup_Body)
						.setBackgroundResource(R.drawable.popup_alert_centerbg);
				mContentView.findViewById(R.id.RL_TwoBtnPopup_Bottom)
						.setBackgroundResource(R.drawable.popup_alert_bottombg);

				dialog.addContentView(mContentView, new LayoutParams(
						LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
				TextView tv = (TextView) mContentView
						.findViewById(R.id.TV_PopupTitle);
				if (mTitle != null)
					tv.setText(mTitle);
				tv = (TextView) mContentView.findViewById(R.id.TV_PopupMessage);
				if (mMessage != null)
					tv.setText(mMessage);
				dialog.setContentView(mContentView);
				Button b = (Button) mContentView.findViewById(R.id.B_Popup_Ok);
				if (mBtnText1 != null)
					b.setText(mBtnText1);
				b.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mOnIKPopupClickListener != null)
							mOnIKPopupClickListener.onIKPopUpClick(v);
					}
				});
				b = (Button) mContentView.findViewById(R.id.B_Popup_Cancel);
				if (mBtnText2 != null)
					b.setText(mBtnText2);
				b.setOnClickListener(new View.OnClickListener() {
					@Override
					public void onClick(View v) {
						if (mOnIKPopupClickListener != null)
							mOnIKPopupClickListener.onIKPopUpClick(v);
					}

				});
				return dialog;
			}

			}
			return null;
		}
	}
}
